package cn.ljy.uem.service.impl;

import cn.ljy.authority.dao.mapper.SysDepartmentMapper;
import cn.ljy.authority.dao.mapper.SysUserMapper;
import cn.ljy.authority.dao.mapper.SysUserRoleMapper;
import cn.ljy.authority.model.SysDepartment;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.utils.password.SHA;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.SchoolDepartment;
import cn.ljy.uem.model.SchoolMajor;
import cn.ljy.uem.model.SchoolPosition;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.model.query.SchoolTeacherQuery;
import cn.ljy.uem.service.SchoolTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;

@Service
@Slf4j
public class ISchoolTeacherService extends IBaseService<SchoolTeacher, SchoolTeacherQuery> implements SchoolTeacherService {

    @Autowired
    private SchoolTeacherMapper mapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;

    @Autowired
    private SchoolDepartmentMapper schoolDepartmentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SchoolCourseMapper schoolCourseMapper;

    @Autowired
    private SchoolMajorMapper schoolMajorMapper;

    @Autowired
    private SchoolPositionMapper schoolPositionMapper;

    @Override
    protected void beforeGet(Serializable id) {


    }

    @Override
    protected void beforeUpdate(SchoolTeacher schoolTeacher) {
        if(schoolTeacher.getDepartmentId() != null|| schoolTeacher.getTeacherNumber()!=null){
            //查找教师的部门
            SchoolDepartment schoolDepartment = new SchoolDepartment();
            schoolDepartment = schoolDepartmentMapper.findById(schoolTeacher.getDepartmentId());
            //查找部门属于那个系统部门
            SysDepartment sysDepartment = new SysDepartment();
            sysDepartment = sysDepartmentMapper.findById(schoolDepartment.getSysDepartmentId());
            if (schoolTeacher.getId() != null && schoolTeacher.getUserId()==null) {               //如果传输进来的学生信息里面没有账户ID 而数据库有 查找存入
                schoolTeacher.setUserId(mapper.findById(schoolTeacher.getId()).getUserId());
            }
            //新增user账户,将教师的信息保存到user表里
            SysUser sysUser = new SysUser();
            sysUser.setId(schoolTeacher.getUserId());
            sysUser.setDepartment(sysDepartment);
            sysUser.setUsername("T"+schoolTeacher.getTeacherNumber());
            sysUser.setPassword(SHA.getResult("123456"));
            sysUser.setNickName(schoolTeacher.getName());
            sysUser.setSex(schoolTeacher.getSex());
            sysUser.setStatus(1);
            log.info(String.valueOf(sysUser.toString()));
            sysUserMapper.update(sysUser);                                                       //如果教师工号获部门改动法修改账户表信息，不更新权限表
        }
    }

    @Override
    protected void beforeSave(SchoolTeacher schoolTeacher) {
        //添加之前的验证
        if (schoolTeacher == null) {
            throw new MyWebException("请确认传入对象是否为空");
        } else {
            if (StringUtils.hasLength(schoolTeacher.getTeacherNumber())) {
                if (mapper.countByTeacherNumber(schoolTeacher.getTeacherNumber()) > 0) {
                    throw new MyWebException("编号" + schoolTeacher.getTeacherNumber() + "的教师已存在");
                } else {
                    //新增user账户,将教师的信息保存到user表里
                    SysUser sysUser = new SysUser();
                    if (schoolTeacher.getDepartmentId()!= null){
                        //查找教师的部门
                        SchoolDepartment schoolDepartment = new SchoolDepartment();
                        schoolDepartment = schoolDepartmentMapper.findById(schoolTeacher.getDepartmentId());
                        //查找部门属于那个系统部门
                        SysDepartment sysDepartment = new SysDepartment();
                        sysDepartment = sysDepartmentMapper.findById(schoolDepartment.getSysDepartmentId());
                        sysUser.setDepartment(sysDepartment);
                    }
                    sysUser.setId(null);
                    sysUser.setUsername("T"+schoolTeacher.getTeacherNumber());
                    sysUser.setPassword(SHA.getResult("123456"));
                    sysUser.setNickName(schoolTeacher.getName());
                    sysUser.setSex(schoolTeacher.getSex());
                    sysUser.setStatus(1);
                    sysUserMapper.save(sysUser);
                    //添加教师
                    if (sysUser.getId() != null) {
                        schoolTeacher.setUserId(sysUser.getId());
                    }
                    //为教师的账户（user表）添加教师角色（-11）
                    sysUserRoleMapper.save(sysUser.getId(),super.getTeacherRoleId());
                }
            }
        }


    }

    @Override
    protected void afterSave(SchoolTeacher schoolTeacher) {
//        System.out.println("添加后的对象");
//        System.out.println(schoolTeacher);
//
//        //向账户表添加教师账户
//        //查找教师对象身上的部门
//        schoolTeacher.setSchoolDepartment( schoolDepartmentMapper.findById(schoolTeacher.getDepartmentId()));
//        System.out.println("学校的部门");
//        System.out.println(schoolTeacher.getSchoolDepartment());
//
//        //通过这个学校身上的部门去查找系统部门
//       //创建部门接收
//        SysDepartment sysDepartment=new SysDepartment();
//        sysDepartment=sysDepartmentMapper.findById (schoolTeacher.getSchoolDepartment().getSysDepartmentId());
//        System.out.println("学校系统部门");
//        System.out.println(sysDepartment);
//
//
////        //将查到的部门设置为user的部门
//        SysUser sysUser=new SysUser();
//        sysUser.setId(null);
//        sysUser.setDepartment(sysDepartment);
//        sysUser.setUsername(schoolTeacher.getName());
//        //设置加密密码
//        sysUser.setPassword(SHA.getResult("123456"));
//        sysUser.setNickName(schoolTeacher.getName());
//        sysUser.setSex(schoolTeacher.getSex());
//        sysUser.setStatus(1);
//        System.out.println(sysUser);
//        int i=sysUserMapper.save(sysUser);
//        System.out.println(i);


    }

    @Override
    public void delete(@NotEmpty(message = "操作失败：请选择要移除的记录") Serializable[] ids) {
        //获取教师记录
        List<Serializable> userIds=mapper.findUserIdsByIds(ids);
        //删除教师记录
        super.delete(ids);
        //删除教师账户
        Serializable[] userIdArray=userIds.toArray(new Serializable[userIds.size()]);
        sysUserRoleMapper.deleteByUserIds(userIdArray);
        sysUserMapper.deleteByIdIn(userIdArray);
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
//            //获取要删除的教师账户id集合
//        Integer TeacherId[] = (Integer[]) ids;
//        for (Integer id : TeacherId) {
//            //取出教师对象上的userid
//            SchoolTeacher schoolTeachers = mapper.findById(id);
//            if (schoolTeachers == null) {
//                throw new MyWebException("该id未找到,请输入正确的id");
//            } else {
//                //创建删除的user表集合
//                Serializable[] userid = {schoolTeachers.getUserId()};
//                sysUserMapper.deleteByIdIn(userid);
//            }
//        }
    }

    @Override
    protected void afterFindAll(List<SchoolTeacher> list) {

        for (SchoolTeacher schoolTeacher : list) {
            if (schoolTeacher.getDepartmentId() != null) {
                //查找教师所属部门
                SchoolDepartment schoolDepartment = schoolDepartmentMapper.findById(schoolTeacher.getDepartmentId());
                //赋值
                schoolTeacher.setSchoolDepartment(schoolDepartment);
            }
            if (schoolTeacher.getUserId() != null) {
                //查找教师对应账号
                SysUser sysUser = sysUserMapper.findById(schoolTeacher.getUserId());
                //赋值
                schoolTeacher.setSysUser(sysUser);
            }

            if (schoolTeacher.getMajorId() != null) {
                //查找教师所属专业
                SchoolMajor schoolMajor = schoolMajorMapper.findById(schoolTeacher.getMajorId());
                //赋值
                schoolTeacher.setSchoolMajor(schoolMajor);
            }
            if (schoolTeacher.getPositionId() != null) {
                //查找教师职务
                SchoolPosition schoolPosition = schoolPositionMapper.findById(schoolTeacher.getPositionId());
                //赋值
                schoolTeacher.setSchoolPosition(schoolPosition);
            }
        }

    }

    @Override
    protected void afterGet(SchoolTeacher schoolTeacher) {
        if (schoolTeacher.getDepartmentId() != null) {
            //查找教师所属部门
            SchoolDepartment schoolDepartment = schoolDepartmentMapper.findById(schoolTeacher.getDepartmentId());
            //赋值
            schoolTeacher.setSchoolDepartment(schoolDepartment);
        }
        if (schoolTeacher.getUserId() != null) {
            //查找教师对应账号
            SysUser sysUser = sysUserMapper.findById(schoolTeacher.getUserId());
            //赋值
            schoolTeacher.setSysUser(sysUser);
        }

        if (schoolTeacher.getMajorId() != null) {
            //查找教师所属专业
            SchoolMajor schoolMajor = schoolMajorMapper.findById(schoolTeacher.getMajorId());
            //赋值
            schoolTeacher.setSchoolMajor(schoolMajor);
        }
        if (schoolTeacher.getPositionId() != null) {
            //查找教师职务
            SchoolPosition schoolPosition = schoolPositionMapper.findById(schoolTeacher.getPositionId());
            //赋值
            schoolTeacher.setSchoolPosition(schoolPosition);
        }
    }

//    @Override
//    protected void afterGet(SchoolTeacher schoolTeacher) {
//        if(schoolTeacher.getSchoolDepartment()!=null&&schoolTeacher.getSchoolDepartment().getId()!=null){
//            SchoolDepartment schoolDepartment =new SchoolDepartment ();
//            schoolDepartment=schoolDepartmentMapper.findById(schoolTeacher.getSchoolDepartment().getId());
//            schoolTeacher.setSchoolDepartment(schoolDepartment);
//        }else {
//            System.out.println("教师id为空，外键字段查询失败");
//        }
//    }

    @Override
    protected void beforeFindAll(SchoolTeacherQuery query) {
//        System.out.println("搜索之前的查找");
//        System.out.println(query.getSchoolDepartment());
    }


}
